﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace Maintain_supplier_information
{
    public partial class ViewStockCard : System.Web.UI.Page
    {
        SupplierBizlogic sbz = new SupplierBizlogic();
        StationeryInventoryEntities1 se = new StationeryInventoryEntities1();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                Calendar1.Visible = false;
                Calendar2.Visible = false;
                List<object> categories = new List<object>();
                categories = sbz.getcategories();

                ddl_category.DataSource = categories;
                ddl_category.DataTextField = "CategoryName";
                ddl_category.DataValueField = "CategoryID";
                ddl_category.DataBind();


            }
        }


        protected void imgbtn_frmdate_Click(object sender, ImageClickEventArgs e)
        {
            Calendar1.Visible = true;
        }

        protected void imgbtn_todate_Click(object sender, ImageClickEventArgs e)
        {
            Calendar2.Visible = true;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            txt_fromDate.Text = Calendar1.SelectedDate.ToShortDateString();
            Calendar1.Visible = false;
        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            txt_toDate.Text = Calendar2.SelectedDate.ToShortDateString();
            Calendar2.Visible = false;
        }

        protected void ddl_category_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sval = Convert.ToInt32(ddl_category.SelectedValue.ToString());
            List<object> catitems = new List<object>();
            catitems = sbz.getCatItems(sval);
            ddl_itemName.DataSource = catitems;
            ddl_itemName.DataTextField = "ItemName";
            ddl_itemName.DataValueField = "ItemCode";
            ddl_itemName.DataBind();

        }

        protected void btn_View_Click(object sender, EventArgs e)
        {
            se = new StationeryInventoryEntities1();
            /*var q = from st in se.Stationeries
                    join c in se.SupplierDetails on st.ItemCode equals c.ItemCode
                    join d in se.Suppliers on c.SupplierID equals d.SupplierID
                    where st.ItemCode==ddl_itemName.DataValueField.ToString()
                    orderby st.ItemCode
                    select new { d.SupplierName, st.ItemName, st.ItemCode, st.UOM, c.Rank };*/
            String itemCode = ddl_itemName.SelectedValue.ToString();


            Stationery sat = se.Stationeries.FirstOrDefault(x => x.ItemCode == itemCode);


            lbl_ic.Text = sat.ItemCode;
            lbl_itemdesc.Text = sat.ItemName;
            lbl_uom_val.Text = sat.UOM;

            List<SupplierDetail> supplierList = (from x in se.SupplierDetails
                                                 where x.ItemCode == itemCode
                                                 orderby x.Rank
                                                 select x).ToList();


            lbl_fsup.Text = supplierList[0].Supplier.SupplierName;
            lbl_secsup.Text = supplierList[1].Supplier.SupplierName;
            lbl_thirdsup.Text = supplierList[2].Supplier.SupplierName;

            string itemCode1 = ddl_itemName.SelectedValue.ToString();
            List<object> list = new List<object>();
            List<StockHistory> ll = new List<StockHistory>();
            var m = from st in se.StockHistories
                    where st.ItemCode == itemCode1
                    select st;

            //{
            //    st.Date,
            //    st.Description,
            //    st.AdjustedQty,
            //    st.Balance

            //};
            //list = m.ToList<object>();
            ll = m.ToList<StockHistory>();
            var n = from ss in ll
                    where ss.Date > Calendar1.SelectedDate && ss.Date < Calendar2.SelectedDate
                    select ss;
            ll = n.ToList<StockHistory>();
            Gdview_Stockcard.DataSource = ll;
            Gdview_Stockcard.DataBind();




        }
    }
}